package com.ca.project.Minio.Util;

import com.ca.project.core.Exception.BusinessException;
import com.ca.project.core.Exception.FrameworkException;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.errors.*;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @PACKAGE_NAME: com.ca.project.Minio.Util
 * @NAME: MinioUtil
 * @USER: ASUS
 * @DATE: 31/12/2024
 * @TIME: 下午2:17
 **/
@Component
@Data
public class MinioUtil {

    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKeyId;
    @Value("${minio.secret}")
    private String secret;
    @Value("${minio.bucketName}")
    private String bucketName;
    @Value("${minio.url}")
    private String url;

    private MinioClient getMinioClient() {
        MinioClient client = new MinioClient.Builder().credentials(accessKeyId, secret).endpoint(endpoint).build();
        return client;
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     * @throws IOException
     * @throws ServerException
     * @throws InsufficientDataException
     * @throws ErrorResponseException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws InvalidResponseException
     * @throws XmlParserException
     * @throws InternalException
     */
    public Map<String, Object> upload(MultipartFile file) throws IOException {
        try {
            if (file == null || file.getSize() == 0)
                throw new BusinessException("文件大小不能为空");
            String filename = file.getOriginalFilename();
            String fileType = file.getContentType();
            InputStream inputStream = file.getInputStream();
            MinioClient client = getMinioClient();

            client.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(filename)
                            .stream(inputStream, file.getSize(), -1)
                            .contentType(fileType)
                            .build());
            HashMap<String, Object> map = new HashMap<>();
            String fileUrl = this.url + "/" + filename;
            map.put("url", fileUrl);
            map.put("key", filename);
            inputStream.close();
            return map;
        } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
            throw new FrameworkException("文件上传失败");
        }
    }

    /**
     * * 删除文件
     * *
     * * @param objectName
     * * @throws ServerException
     * * @throws InsufficientDataException
     * * @throws ErrorResponseException
     * * @throws IOException
     * * @throws NoSuchAlgorithmException
     * * @throws InvalidKeyException
     * * @throws InvalidResponseException
     * * @throws XmlParserException
     * * @throws InternalException
     */
    public boolean deleteFile(String objectName) {
        try {
            MinioClient client = getMinioClient();
            client.removeObject(RemoveObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .build());
            return true;
        } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
            throw new BusinessException("删除失败");
        }
    }

}
